package d6_thread_pool;

import java.util.concurrent.*;

/**
 * 2024/6/24 掌握线程池的创建 Cjj 9:44
 */
public class ThreadPollTest1 {
    public static void main(String[] args) {
      //1.通过ThreadPoolExecutor创建一个线程池对象
            /* public ThreadPoolExecutor(int corePoolSize,
                                        int maximumPoolSize,
                                        long keepAliveTime,
                                        TimeUnit unit,
                                        BlockingQueue<Runnable> workQueue,
                                        ThreadFactory threadFactory,
                                        RejectedExecutionHandler handler)*/

        /*new ArrayBlockingQueue<>(4) 任务队列只能缓存4个任务*/
        /*ThreadFactory threadFactory 线程工程，为线程池创建工场*/
        ExecutorService pool  = new ThreadPoolExecutor(3, 5, 8,
                   TimeUnit.SECONDS, new ArrayBlockingQueue<>(4),Executors.defaultThreadFactory(),
                   new ThreadPoolExecutor.AbortPolicy());

        MyRunnable target = new MyRunnable();
        pool.execute(target); //线程池会自动创建一个新线程,自动处理这个任务,自动执行的
        pool.execute(target);//线程池会自动创建一个新线程,自动处理这个任务,自动执行的
        pool.execute(target);//线程池会自动创建一个新线程,自动处理这个任务,自动执行的
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);

        //到了临时线程的创建时机了
        pool.execute(target);
        pool.execute(target);

        //到了新任务的拒绝时机了
        pool.execute(target);
        //pool.shutdown();//立即关闭线程池

    }
}
